﻿using System.Collections.Generic;
using UnityEngine;

///<summary>
///
///<summary>

public class MapCreation : MonoBehaviour
{
    /// <summary>
    /// 最大敌人数
    /// </summary>
    public int maxCount = 5;
    private int spawnedCount;
    private int starCount = 1;
    //0、家 1、墙 2、障碍 3、出生效果 4、河流 5、草 6、空气墙 7.出生效果2
    public GameObject[] item;
    //已有东西的位置列表
    private List<Vector3> itemPositionList = new List<Vector3>();
    private void Awake()
    {
        initialization();
    }

    private void initialization()
    {
        //实例化heart
        CreateItem(item[0], new Vector3(0, -8, 0), Quaternion.identity);
        //实例化墙
        CreateItem(item[1], new Vector3(1, -8, 0), Quaternion.identity);
        CreateItem(item[1], new Vector3(-1, -8, 0), Quaternion.identity);
        for (int i = -1; i < 2; i++)
        {
            CreateItem(item[1], new Vector3(i, -7, 0), Quaternion.identity);
        }
        //实例化空气墙
        for (int i = -11; i < 12; i++)
        {
            CreateItem(item[6], new Vector3(i, 9, 0), Quaternion.identity);
            CreateItem(item[6], new Vector3(i, -9, 0), Quaternion.identity);
        }
        for (int i = -8; i < 9; i++)
        {
            CreateItem(item[6], new Vector3(-11, i, 0), Quaternion.identity);
            CreateItem(item[6], new Vector3(11, i, 0), Quaternion.identity);
        }
        //实例化地图
        for (int i = 0; i < 60; i++)
        {
            CreateItem(item[1], CreateRandomPosition(), Quaternion.identity);
        }
        for (int i = 0; i < 20; i++)
        {
            CreateItem(item[2], CreateRandomPosition(), Quaternion.identity);
            CreateItem(item[4], CreateRandomPosition(), Quaternion.identity);
            CreateItem(item[5], CreateRandomPosition(), Quaternion.identity);
        }
        //实例化玩家1
        GameObject go = Instantiate(item[3], new Vector3(-2, -8, 0), Quaternion.identity);
        go.GetComponent<Born>().createPlayer = true;
        //GameObject go1 = Instantiate(item[7], new Vector3(2, -8, 0), Quaternion.identity);
        //go1.GetComponent<Born>().createPlayer = true;
        //产生敌人
        CreateItem(item[3], new Vector3(-10, 8, 0), Quaternion.identity);
        CreateItem(item[3], new Vector3(0, 8, 0), Quaternion.identity);
        CreateItem(item[3], new Vector3(10, 8, 0), Quaternion.identity);
        InvokeRepeating("CreateEnemy", 4, 5);


        //InvokeRepeating("CreateEnemy", 4, 5);
    }
    //private void Start()
    //{
    //    for (int i = 0; i < starCount; i++)
    //    {
    //        spawnedCount++;
    //        if (spawnedCount >= maxCount)
    //        {
    //            return;
    //        }
    //        InvokeRepeating("CreateEnemy", 4, 5);
    //    }
    //}

    private void CreateItem(GameObject createGamObject, Vector3 createPostion, Quaternion createRotation)
    {
        GameObject itemGo = Instantiate(createGamObject, createPostion, createRotation);
        itemGo.transform.SetParent(gameObject.transform);
        itemPositionList.Add(createPostion);
    }
    //产生随机位置
    private Vector3 CreateRandomPosition()
    {
        //不生成x=10,10的两列，y=-8,8两行的位置
        while (true)
        {
            Vector3 createPosition = new Vector3(Random.Range(-9, 10), Random.Range(-7, 8));
            if (!HasThePosition(createPosition))
            {
                return createPosition;
            }

        }
    }
    //判断位置列表中是否有这个位置
    private bool HasThePosition(Vector3 createPos)
    {
        for (int i = 0; i < itemPositionList.Count; i++)
        {
            if (createPos == itemPositionList[i])
            {
                return true;
            }
        }
        return false;
    }
    //产生敌人
    private void CreateEnemy()
    {
        int num = Random.Range(0, 3);
        Vector3 EnemyPos = new Vector3();
        if (num == 0)
        {
            EnemyPos = new Vector3(-10, 8, 0);
        }
        else if (num == 1)
        {
            EnemyPos = new Vector3(0, 8, 0);
        }
        else
        {
            EnemyPos = new Vector3(10, 8, 0);
        }
        CreateItem(item[3], EnemyPos, Quaternion.identity);

    }
}
